<?php
require('SendSms.php');

session_start();
include_once("../../untils/conn.php");
mysqli_query($con, "set names utf8");

// 获取当前正在登录的账号信息的代理
$proxy_acc = $_SESSION["account"];

// 查询代理的proxy_upid
$sql = "SELECT proxy_upid FROM proxy WHERE proxy_acc = '$proxy_acc'";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
$proxy_upids = $row['proxy_upid'];

// 查询代理的proxy_acc
$sql = "SELECT proxy_acc FROM proxy WHERE proxy_id = '$proxy_upids'";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
$proxy_accs = $row['proxy_acc'];

// 存储为变量
$proxy_upid = $proxy_accs;

// echo  $proxy_upid;

$goods = $_POST['goods'];
$name = $_POST['name'];
$uid = $_POST['uid'];
$phone = $_POST['phone'];
$sf = $_POST['sf'];
$city = $_POST['city'];
$xian = $_POST['xian'];
$dizhi = $_POST['dizhi'];
$oderid = $_POST['oderid'];
$time = $_POST['time'];
$price =$_POST['price'];
$proxy_id = $_POST['proxyid'];
$proxy_price = $_POST['prices'];
$proxy_prices = $_POST['proxy_pricesss'];
$beizhu = "订单号:" . $oderid . "返佣￥" . $proxy_prices;
//  echo $beizhu;

include_once("../../untils/conn.php");
if ($con) {
    mysqli_query($con, "set names utf8");
    $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
    $count = mysqli_num_rows($data_query);

    if ($count > 3) {
        // 用户已经申请过了，返回状态码1和提示信息
        $response = array('status' => 1, 'msg' => '您申请的卡数已超限！请联系客服！');
        echo json_encode($response);
    } else {
        $data_insert = mysqli_query($con, "insert into oder(goods,name,uid,phone,sf,city,xian,dizhi,oderid,time,proxy_id,proxy_price,proxy_prices,price) values('$goods','$name','$uid','$phone','$sf','$city','$xian','$dizhi','$oderid','$time','$proxy_id','$proxy_price','$proxy_prices','$price')");
        
        // echo  $data_insert;
        // echo "SQL: " . $data_insert . "<br>";
        
        if ($data_insert) {
            // 申请成功，返回状态码0和提示信息
            $response = array('status' => 0, 'msg' => '申请成功');
            echo json_encode($response);
            
            // 判断$status是否为0，如果是则执行$sendSms()函数
            if ($response['status'] == 0) {
                // 写入佣金记录表
                $data_revenue = mysqli_query($con, "insert into revenue(account,oderid,name,price,beizhu,cr_time,proxy_upid,proxy_prices) values('$proxy_acc','$oderid','$name','$proxy_prices','$beizhu','$time','$proxy_upid','$proxy_price')");


                //发送短信通知
                sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
                
            }
        } else {
            // 信息不全，返回状态码-1和提示信息
            $response = array('status' => -1, 'msg' => '信息不全，请输入全部信息');
            echo json_encode($response);
        }
    }
}

